package com.zhangliang.oa.crud;

import com.zhangliang.oa.bean.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.*;

@WebServlet("/dept/welcome")
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contextPath = request.getContextPath();
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
        for (Cookie cookie : cookies) {
            if (cookie != null){
                String name = cookie.getName();
                if ("username".equals(name)){
                    username = cookie.getValue();
                    //System.out.println(username);
                }else if ("password".equals(name)){
                    password = cookie.getValue();
                    //System.out.println(password);
                }
            }
        }
        if (username != null && password !=  null){
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_user","root","802398");
                ps = conn.prepareStatement("select * from t_user where username = ? and password = ?");
                ps.setString(1,username);
                ps.setString(2,password);
                rs = ps.executeQuery();
                if (rs.next()){
                    HttpSession session = request.getSession();
                    User user = new User(username,password);
                    //System.out.println(user.getPassword());
                    session.setAttribute("user",user);
                    //session.setAttribute("username",username);
                    response.sendRedirect(contextPath+"/dept/list");
                } else {
                    response.sendRedirect(contextPath+"/error.jsp");
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            response.sendRedirect(contextPath+"/index.jsp");
        }
    }
}
